<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/9
 * Time: 10:14
 */
namespace app\api\controller;
use think\Db;
class Index extends Base {

    /**
     * 异地登录验证
     * @return string
     */
//    public function __construct()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == 112){
//            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//            exit;
//        }elseif($auth == 110){
//            echo json_encode(['status' => 302, 'msg' => $this->frost]);
//            exit;
//        }
//    }
    //首页
    public function index(){
        extract(input());
        $param = input('');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where = array();
        if($param['sex']==1){//男
            $where['uuu.user_gender'] =1;
        }elseif ($param['sex']==2){//女
            $where['uuu.user_gender'] =2;
        }elseif ($param['sex']==3){
            $where= array();
        }

        if($param['type']==1){//附近
            $field = 'a.wall_id,a.wall_sheng,uuu.user_gender,uuu.user_last_unline_time,uuu.user_id,uuu.user_birthday,uuu.user_username,uuu.user_const,a.wall_lon,a.wall_lat,a.wall_img,a.wall_video,a.wall_user_id,a.wall_type,
        ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$param['lat'].'* PI() / 180 - a.wall_lat * PI() / 180) / 2),2) + COS('.$param['lat'].' * PI() / 180) * COS(a.wall_lat * PI() / 180) * POW(SIN(('.$param['lon'].' * PI() / 180 - a.wall_lon * PI() / 180) / 2),2))) * 1000) AS distance';
            $order = 'distance asc,a.wall_add_time desc';
            $list = Db::table('up_wall')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.wall_user_id','left')
//                ->join('up_area ua', 'ua.district_id = a.wall_shi', 'left')
                ->where($where)
                ->field($field)
                ->limit($limit,$num)
                ->order($order)
//                ->group('a.wall_user_id')
                ->select();
            foreach ($list as $key=>&$vo){
                $vo['age'] = $this->getAge($vo['user_birthday']);
                //查询自己和发布者
                $map['user_id'] = $user_id;
                $map['other_user_id'] = $vo['wall_user_id'];
                $map['friend_status'] = 2;
                //查询发布者和自己
                $map1['user_id'] = $vo['wall_user_id'];
                $map1['other_user_id'] = $user_id;
                $map1['friend_status'] = 2;
                $info = Db::table('up_friend')->where($map)->find();
                $info1 = Db::table('up_friend')->where($map1)->find();
                if($info['friend_id']!=''&&$info1['friend_id']!=''){
                    $vo['is_friend'] = 1;
                }else{
                    $vo['is_friend'] = 2;
                }
                if($vo['user_gender']==2){
                    $vo['vip'] = 1;
                }else{
                    $vo['vip'] = $this->getVip($vo['user_id']);
                }
            }
        }elseif ($param['type']==2){//活跃
            $field = 'a.wall_sheng,uuu.user_id,uuu.user_gender,uuu.user_last_unline_time,uuu.user_birthday,uuu.user_username,uuu.user_const,a.wall_img,a.wall_video,a.wall_user_id,a.wall_type';
            $order = 'a.wall_add_time desc';
            $list = Db::table('up_wall')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.wall_user_id','left')
//                ->join('up_area ua', 'ua.district_id = a.wall_shi', 'left')
                ->where($where)
                ->field($field)
                ->limit($limit,$num)
                ->order($order)
//                ->group('a.wall_user_id')
                ->select();
            foreach ($list as $key=>&$vo){
                $vo['age'] = $this->getAge($vo['user_birthday']);
                //查询自己和发布者
                $map['user_id'] = $user_id;
                $map['other_user_id'] = $vo['wall_user_id'];
                $map['friend_status'] = 2;
                //查询发布者和自己
                $map1['user_id'] = $vo['wall_user_id'];
                $map1['other_user_id'] = $user_id;
                $map1['friend_status'] = 2;
                $info = Db::table('up_friend')->where($map)->find();
                $info1 = Db::table('up_friend')->where($map1)->find();
                if($info['friend_id']!=''&&$info1['friend_id']!=''){
                    $vo['is_friend'] = 1;
                }else{
                    $vo['is_friend'] = 2;
                }
                if($vo['user_gender']==2){
                    $vo['vip'] = 1;
                }else{
                    $vo['vip'] = $this->getVip($vo['user_id']);
                }
            }
        }elseif ($param['type']==3){//新人
            $field = 'a.wall_sheng,uuu.user_id,uuu.user_gender,uuu.user_last_unline_time,uuu.user_birthday,uuu.user_username,uuu.user_const,a.wall_img,a.wall_video,a.wall_user_id,a.wall_type';
            $order = 'uuu.user_time desc';
            $list = Db::table('up_wall')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.wall_user_id','left')
//                ->join('up_area ua', 'ua.district_id = a.wall_shi', 'left')
                ->where($where)
                ->field($field)
                ->limit($limit,$num)
                ->order($order)
//                ->group('a.wall_user_id')
                ->select();
            foreach ($list as $key=>&$vo){
                $vo['age'] = $this->getAge($vo['user_birthday']);
                //查询自己和发布者
                $map['user_id'] = $user_id;
                $map['other_user_id'] = $vo['wall_user_id'];
                $map['friend_status'] = 2;
                //查询发布者和自己
                $map1['user_id'] = $vo['wall_user_id'];
                $map1['other_user_id'] = $user_id;
                $map1['friend_status'] = 2;
                $info = Db::table('up_friend')->where($map)->find();
                $info1 = Db::table('up_friend')->where($map1)->find();
                if($info['friend_id']!=''&&$info1['friend_id']!=''){
                    $vo['is_friend'] = 1;
                }else{
                    $vo['is_friend'] = 2;
                }
                if($vo['user_gender']==2){
                    $vo['vip'] = 1;
                }else{
                    $vo['vip'] = $this->getVip($vo['user_id']);
                }
            }
        }
        foreach ($list as $key=>&$vo){
            $vo['time'] = $this->formatTime(strtotime($vo['user_last_unline_time']));
        }
        return  json(array('status'=>200,'msg'=>$list));
    }
    //礼物
    public function gift(){
        extract(input());
        $list =  Db::table('up_gift')->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //账户金币
    public function account(){
        extract(input());
        $list = Db::table('up_u_user')->where('user_id',$user_id)->field('user_balance')->find();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //添加好友
    public function addFriend(){
        extract(input());
        $gift_id = input('param.gift_id');
        $glod = input('param.glod');
        $content = input('param.content');
        $privately = input('param.privately');
        $num = input('param.num');
        $other_user_id = input('param.other_user_id');
        $sex = input('param.sex');

        $map['user_id'] = $user_id;
        $map['other_user_id'] = $other_user_id;
        $info = Db::table('up_friend')->where($map)->find();
        if($info['friend_id']!=''){
            return  json(array('status'=>100,'msg'=>"添加好友请求已经发送,等待她人确认"));
        }
        //查询这个用户
        $list = Db::table('up_u_user')->where('user_id',$user_id)->find();
       if($list['user_gender']==1&&$sex==2){//只有申请人是男的，被申请人是女的
           //查询这个用户的余额
           $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
           if($glod>$user_balance){
               return  json(array('status'=>100,'msg'=>"金币不足"));
           }else{
               //扣除用户余额
               Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$glod);
//               if($sex ==2){//增加女性用户送礼物金币
//                   Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_gift_gold',$glod);
//               }
           }
       }

        $data['user_id'] = $user_id;
        $data['other_user_id'] = $other_user_id;
        $data['gift_id'] = $gift_id;
        $data['friend_glod'] = $glod;
        $data['friend_content'] = $content;
        $data['privately'] = $privately;
        $data['friend_num'] = $num;
        $data['friend_time'] = date('Y-m-d H:i:s',time());

        $res = Db::table('up_friend')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }

    }

    //去过哪些国家
    public function address(){
        extract(input());
        $address = input('param.address');
        $where['go_user_id'] = $user_id;
        $where['go_address_name'] = $address;
        $res = Db::table('up_go_address')->where($where)->find();
        if($res['go_address_id']==''){//证明这个国家没有去过
            $data['go_user_id'] = $user_id;
            $data['go_address_name'] = $address;
            Db::table('up_go_address')->insertGetId($data);
        }
        return  json(array('status'=>200,'msg'=>"成功"));
    }

    //获取用户信息
    public function getUserInfo(){
        extract(input());
        $id = input('param.id');
        $id_array = explode(',',$id);
        $list = array();
        foreach ($id_array as $key=>$vo){
           $info = Db::table('up_u_user')
                ->where('user_id',$vo)
                ->field('user_id,user_username,user_gender,user_image')->find();
            if($info['user_gender'] == 1){
                $info['vip'] = $this->getVip($info['user_id']);
            }else{
                $info['vip'] = 1;
            }
            //查询自己和发布者
            $map['user_id'] = $user_id;
            $map['other_user_id'] = $vo;
            $map['friend_status'] = 2;
            //查询发布者和自己
            $map1['user_id'] = $vo;
            $map1['other_user_id'] = $user_id;
            $map1['friend_status'] = 2;
            $info1 = Db::table('up_friend')->where($map)->find();
            $info2 = Db::table('up_friend')->where($map1)->find();
            if($info1['friend_id']!=''&&$info2['friend_id']!=''){
                $info['is_friend'] = 1;
            }else{
                $info['is_friend'] = 2;
            }
            $list[$key] = $info;
        }
        return  json(array('status'=>200,'msg'=>$list));
    }
}